$('.datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "product/list",
		"data" : function(d) {
            var query = $("#query").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
	     {
	    	 render : function(data,type, row, meta) {
	    		 if(row["parentId"]){
	    			 return row["name"];
	    		 }else{
	    			 return '<span style="color:red;">'+row["name"]+'</span>';
	    		 }
	    	}			
	     },
         {
        	 render : function(data, type, row, meta) {
        		 if(row.price == null){
        			 return "0.00";
        		 }else{
        			 return row.price;
        		 }
        	 }
         },
         { "data": "parentName" },
         { "data": "productDesc" },
         {
            render : function(data,type, row, meta) {
    			 return $("#btnGroup").html();
            }
        }
     ],
     "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			$("#edit").validate().resetForm();
		    $("#edit").fill($('.datatable-basic').DataTable().row($(this).closest("tr")).data());
		    $(".select").trigger("change");
		    
		    var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
		    $(":input[name='id']").val(row.id);
		    if(row.parentId){
			    $("#parentId").val(row.parentId);
			 	$("#parentId").trigger("change");
		    }else{
		    	 $("#parentId").val("");
				 	$("#parentId").trigger("change");
		    }
		 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			var text = "确定要删除产品" + row.name + "?";
			if(!row["parentId"]){
				text = "该产品" + row.name + "为父类产品，确定删除？";
			}
			swal({
				title : "修改状态",
				text : text,
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "product/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除产品" +  row.name  + "成功","", "success");
				})
			});
			
		 });
      }
});

$(function(){
	displayName = $("#displayName").val();
	
	$(".btn-add").click(function(){
		$("#edit").validate().resetForm();
		$("#id").val("");
		$("#edit")[0].reset();
		$("#parentId").val("");
	 	$("#parentId").trigger("change");
	});
	
	$("#submitEdit").click(function(){
		if(!$("#parentId").val()){
			$("#price").attr("required",false);
	 	}
		
		if($("#edit").valid()){
			$.post(contextpath + "product/save", $("#edit").serialize(), function(){
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑产品成功","", "success");
			})
		}
	});
	 
	$("#submit").click(function(){
		$('.datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});
})
